#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/wait.h>
#include<unistd.h>

//实现 | 管道符
int main()
{
  int pipefd[2]={0};
  int res = pipe(pipefd);
  if(res<0)
  {
    perror("pipe error");
    return -1;
  }
  pid_t ret = fork();
  if(ret==0)
  { 
    //将标准输出重定向到写入端
    close(pipefd[0]);
    dup2(pipefd[1],1);
    execlp("ps","ps","-ef",NULL);
    exit(0);
  }

  ret=fork();
  if(ret==0)
  {
    //将标准输出重定向到输出端
    close(pipefd[1]);
    dup2(pipefd[0],0);
    execlp("grep","grep","pipe1",NULL);
    exit(0);
  }
  close(pipefd[0]);
  close(pipefd[1]);
  wait(NULL);
  wait(NULL);
  return 0;
}
